package com.tiny.work.test.proxy;

import com.google.common.collect.Lists;
import com.tiny.work.test.jdk.proxy.ProxyHandler;
import lombok.Data;

import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.List;

interface Subject {
    public void doSomething(Request request);
}

@Data
class Request {
    private int id;
    private List<Integer> list = Lists.newArrayList(5,2,4);
    public Request(int id){
        this.id =id;
    }

    @Override
    public String toString() {
        Collections.sort(list);
        return "Request{" +
                "id=" + id +
                ", list=" + list +
                '}';
    }
}

public class RealSubject implements Subject {
    public void doSomething(Request request) {
        System.out.println("call doSomething()");
    }

    public static void main(String... args) {
        RealSubject real = new RealSubject();
        Subject proxySubject = (Subject) Proxy.newProxyInstance(Subject.class.getClassLoader(),
                new Class[]{Subject.class},
                new ProxyHandler(real));
        Request request = new Request(1);
        proxySubject.doSomething(request);
    }
}